<template>
  <el-dialog
    :title="dialog.title"
    :visible.sync="dialog.show"
    width="530px"
    custom-class="dialogForm"
    append-to-body
    @close="handleClose"
  >
    <div class="dialog_context">
      <!-- ref="adjustForm" -->
      <el-form :model="form" label-width="150px">
        <el-form-item label="选中服务星数量: ">
          <el-input
            v-model="form.selectionCount"
            :disabled="true"
            style="width:200px;"
            size="medium"
          />
        </el-form-item>
        <el-form-item label="专属客服: ">
          <el-select
            v-model="form.seatTableName"
            :remote-method="remoteMethod"
            placeholder="请输入关键词"
            filterable
            size="medium"
          >
            <el-option
              v-for="item in kfTableSeatList"
              :key="item.seatTableId"
              :label="item.kfNickname"
              :value="item.seatTableId"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="adjustClick">确定</el-button>
          <el-button style="margin-left:50px; margin-top:10px;" @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import { selectAllKfSeatTable, adjust } from '@/api/serviceStarManager'

export default {
  props: {
    // 父组件调用时传进来的参数
    modalData: {
      type: Object,
      selectionData: Object,
      default: () => {}
    }
  },
  data() {
    return {
      dialog: {
        title: '客服调整',
        show: true
      },
      form: {
        seatTableName: '',
        selectionCount: this.modalData.selectionData.length
      },
      kfTableSeatList: [] // 专属客服列表
    }
  },
  mounted() {
    // 初始化下拉框的值
    this.selectAllSeatTable()
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    /**
     * 调整事件
     */
    adjustClick() {
      // 1. list里面的id是当前服务星的id
      // 需要通过服务星的id去修改第三张表的坐席id和服务星表的调整
      // 2. 客服表的Id(下拉框展示)
      const list = []
      this.modalData.selectionData.forEach(item => {
        const obj = {}
        obj.serviceStarId = item.serviceStarId
        obj.kfIsAdjust = 'Y'
        list.push(obj)
      })
      var formData = new FormData()
      formData.append('list', JSON.stringify(list))
      formData.append('formValue', this.form.seatTableName)
      if (this.form.seatTableName === '') {
        this.$message({
          showClose: true,
          message: '请选择一个客服进行操作',
          type: 'warning'
        })
        return
      } else {
        adjust(formData).then(res => {
          if (res.data.data === true) {
            this.$message({
              showClose: true,
              message: '操作成功'
            })
          } else {
            this.$message({
              showClose: true,
              message: '操作异常,请联系管理员',
              type: 'warning'
            })
          }
          this.closeDialog()
          this.modalData.shutDownModelAndRefresh()
        })
      }
    },
    closeDialog() {
      this.$emit('close')
    },
    selectAllSeatTable() {
      selectAllKfSeatTable(this.form.seatTableName).then(res => {
        this.kfTableSeatList = res.data.data
      })
    },
    remoteMethod(keyWord) {
      selectAllKfSeatTable(keyWord).then(res => {
        this.kfTableSeatList = res.data.data
      })
    }
  }
}
</script>
